/**
 * 初始化页面
 */
$(function () {
    var id = sessionStorage.getItem('id');
    // console.log(id);
    //发异步，从Java获取数据
    $.ajax({
        type: 'get',
        url: '/user/findById/'+id,
        success: function (data) {
            if (data.message == "success") {
                // 存储获取的邮箱，进行是否修改判断
                sessionStorage.setItem('email',data.object.email);
                //赋值
                $("#L_id").val(data.object.id);
                $("#L_username").val(data.object.username);
                $("#L_email").val(data.object.email);
                $("#birthday").val(data.object.birthday);
                $("#L_phone").val(data.object.phone);
                $("#L_site").val(data.object.site);
                // 获取到性别值进行匹配
                $("#sex :radio").each(function () {
                    var sex = $(this);
                    if (sex.val() == data.object.sex) {
                        sex.prop("checked",true);
                        layui.form.render();
                    }
                })
                // 获取到爱好值进行匹配
                let hobby = data.object.hobby.split(',');
                for (let i = 0; i < hobby.length; i++) {
                    $("#hobby :checkbox").each(function () {
                        if (this.title == hobby[i]) {
                            $(this).prop("checked",true);
                            layui.form.render();
                        }
                    })
                }
                return false;
            }
            // console.log(data);
            layer.msg('信息获取失败，请稍后重试！',{icon:0,time:1200});
            return false;
        },
        error: function () {
            layer.msg('请求接口异常！', {icon: 0, time: 1200});
            return false;
        }

    });
});

layui.use(['form', 'layer', 'laydate', 'jquery'], function () {
    $ = layui.jquery;
    var form = layui.form,
        laydate = layui.laydate;
    layer = layui.layer;

    //执行一个laydate实例
    laydate.render({
        elem: '#birthday', //指定元素
        trigger: 'click' //采取事件弹出
    });

    //自定义验证规则
    form.verify({
        username: function (value) {
            if (value.length < 2) {
                return '昵称至少得2个字符';
            }
        },
    });

    //监听提交
    form.on('submit(add)', function (data) {
        // 爱好是否选中
        var arr_box = [];
        $("#hobby :checkbox").each(function () {
            var isCheck = $(this).prop("checked");
            if ('checked' == isCheck || isCheck) {
                arr_box.push(this.title);
            }
        });
        if (arr_box.length == 0) {
            layer.msg('请选择爱好哦', {icon: 2, time: 1000});
            return false;
        }
        // 进行引用赋值
        data.field.hobby = arr_box.toString();
        // console.log(data.field);
        //发异步，把数据提交给Java
        $.ajax({
            type: 'post',
            url: '/user/updateMessage',
            data: JSON.stringify(data.field),
            contentType: 'application/json;charset=utf-8',
            dataType: 'json',
            success: function (res) {
                if (res.message == "success") {
                    $("#sumbit").attr("disabled",true); //禁止点击按钮
                    layer.msg('修改成功！', {icon: 1, time: 1100,end:function () {
                            // 可以对父窗口进行刷新
                            xadmin.father_reload();
                        }});
                    return false;
                }
                layer.msg('修改失败！', {icon: 2, time: 1200});
                return false;
            },
            error: function () {
                layer.msg('请求接口异常！', {icon: 0, time: 1200});
                return false;
            }

        });
        return false;
    });

});

/**
 * 验证邮箱是否已经存在
 */
function vreifyEmail() {
    var sessionEmail = sessionStorage.getItem('email');
    var email = $("#L_email").val();
    var emailzz = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (emailzz.test(email)) {
        if (email != sessionEmail) {
            $.get("/user/findByEmail",{'email':email},function (data) {
                if (data.message == "success") {
                    $("#emailHint").attr('style','color:red');
                    $("#emailHint").html('邮箱已被使用，请更换邮箱！');
                    $("#sumbit").attr('disabled',true);//提交按钮失效
                    return false;
                }
                $("#emailHint").attr('style','color:green');
                $("#emailHint").html('此邮箱将会成为您唯一的登入名！');
                $("#sumbit").attr('disabled',false); //提交按钮恢复
                return false;
            })
        }
    }
    return false;
}

/**
 * 重置会员编辑表单信息为原始信息
 */
function resetUserEdit() {
    var id = sessionStorage.getItem('id');
    // console.log(id);
    //发异步，从Java获取数据
    $.ajax({
        type: 'get',
        url: '/user/FindByIdOrUpdateMessage',
        data: {'id':id},
        success: function (data) {
            if (data.message == "success") {
                // 存储获取的邮箱，进行是否修改判断
                sessionStorage.setItem('email',data.email);
                //赋值
                $("#L_id").val(data.object.id);
                $("#L_username").val(data.object.username);
                $("#L_email").val(data.object.email);
                $("#birthday").val(data.object.birthday);
                $("#L_phone").val(data.object.phone);
                $("#L_site").val(data.object.site);
                // 获取到性别值进行匹配
                $("#sex :radio").each(function () {
                    var sex = $(this);
                    if (sex.val() == data.sex) {
                        sex.prop("checked",true);
                        layui.form.render();
                    }
                })
                // 获取到爱好值进行匹配
                let hobby = data.object.hobby.split(',');
                for (let i = 0; i < hobby.length; i++) {
                    $("#hobby :checkbox").each(function () {
                        if (this.title == hobby[i]) {
                            $(this).prop("checked",true);
                            layui.form.render();
                        }
                    })
                }
                return false;
            }
            // console.log(data);
            layer.msg('重置失败，请稍后重试！',{icon:0,time:1200});
            return false;
        },
        error: function () {
            layer.msg('请求接口异常！', {icon: 0, time: 1200});
            return false;
        }

    });
}

var _hmt = _hmt || [];
(function () {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
})();